<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bluemobi.ssm.data.${toClassName}Mapper">
    <insert id="save" parameterType="${toClassName}" <#if hasFieldAutoIncrease>useGeneratedKeys="true" keyProperty="${fieldAutoIncrease}"</#if>>
        insert into ${name}(${fieldString}) values(${fieldWrapString})
    </insert>

    <select id="selectAll" resultType="${toClassName}">
        select * from ${name}
    </select>

    <select id="selectByCondition" parameterType="${toClassName}" resultType="${toClassName}">
        select * from ${name}
        <where>
        <#list fields as field>
            <if test="${field.name}!=null">and ${field.name}=${r"#{"}${field.name}} </if>
        </#list>
        </where>
        <if test="pager!=null"> limit ${r"#{"}pager.offset},${r"#{"}pager.numPerPage}</if>
    </select>

    <select id="selectTotal" parameterType="${toClassName}" resultType="long">
        select count(*) from ${name}
        <where>
        <#list fields as field>
            <if test="${field.name}!=null">and ${field.name}=${r"#{"}${field.name}} </if>
        </#list>
        </where>
    </select>

    <select id="selectSingle" parameterType="${primeType}" resultType="${toClassName}">
        select * from ${name}
        <where>
        <#if primeType!=toClassName>
            <#if hasCompositePk>
                <#list primtives as prikey>
                    <if test="${prikey.name}!=null">and ${prikey.name}=${r"#{"}${prikey.name}} </if>
                </#list>
            <#else>
                <#list primtives as prikey>
                    and ${prikey.name}=${r"#{"}${prikey.name}}
                </#list>
            </#if>
        <#else>
            <#list fields as field>
                <if test="${field.name}!=null">and ${field.name}=${r"#{"}${field.name}} </if>
            </#list>
        </#if>
        </where>
    </select>

    <update id="update" parameterType="${toClassName}">
        update ${name}
        <set>
        <#list fields as field>
            <#if field.name!=fieldPrime>
                <if test="${field.name}!=null">${field.name}=${r"#{"}${field.name}},</if>
            </#if>
        </#list>
        </set>
        <where>
        <#list primtives as prikey>
            <if test="${prikey.name}!=null">and ${prikey.name}=${r"#{"}${prikey.name}} </if>
        </#list>
        </where>
    </update>

    <delete id="delete" parameterType="${primeType}" >
        delete from ${name}
        <where>
        <#if primeType!=toClassName>
            <#if hasCompositePk>
                <#list primtives as prikey>
                    <if test="${prikey.name}!=null">and ${prikey.name}=${r"#{"}${prikey.name}} </if>
                </#list>
            <#else>
                <#list primtives as prikey>
                    and ${prikey.name}=${r"#{"}${prikey.name}}
                </#list>
            </#if>
        <#else>
            <#list fields as field>
                <if test="${field.name}!=null">and ${field.name}=${r"#{"}${field.name}} </if>
            </#list>
        </#if>
        </where>
    </delete>
</mapper>